<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 15/11/21
 * Time: 下午9:50
 */
class User_level extends MY_Agent_Controller
{
    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;

        $this->load->model('Model_user_level_config', 'lc');

        $totalNum = $this->lc->count();
        $levelInfo = $this->lc->lists($offset, $limit);


        $this->load->library('pagination');
        $config = $this->page_config('User_level/index', $totalNum, $limit);
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $data = array(
            'title' => '用户等级列表',
            'level_info' => $levelInfo,
            'pageHtml' => $pageHtml,
            'totalNum' => $totalNum,
        );


        $this->load->view('agent/user_level/index', $data);
    }

    public function create()
    {
        $lid = (int)$this->uri->segment(3, 0);


        $data = array(
            'title' => $lid ? '编辑用户等级' : '添加用户等级',
        );


        if ($lid)
        {
            $this->load->model('Model_user_level_config', 'lc');
            $data['data'] = $this->lc->getById($lid);
            $data['lid'] = $lid;
        }


        $this->load->view('agent/user_level/create', $data);
    }

    public function save()
    {
        $lid          = (int)$this->input->get_post('lid');


        $data = array(
            'name'              => trim($this->input->get_post('name')),
            'level'             => (int)$this->input->get_post('level'),
            'friend_num'        => (int)$this->input->get_post('friend_num'),
            'complate_task_num' => $this->input->get_post('complate_task_num'),
            'input_amount'      => fPrice($this->input->get_post('input_amount'), 4),
            'reward'            => fPrice($this->input->get_post('reward'), 4),
            'logo'              => (int)$this->input->get_post('logo'),
            'descr'             => trim($this->input->get_post('descr')),
        );


        if (empty ($data['name']) || empty ($data['level']) || empty ($data['friend_num']) ||
            empty ($data['input_amount']) || empty ($data['reward']) || empty ($data['logo']))
            show_error('参数错误!');


        $this->load->model('Model_user_level_config', 'lc');


        if (!$lid)
        {
            $item = $this->lc->getOne(array('name' => $data['name']));
            if (!empty ($item))
                show_error('此等级名称已经存在!');


            $item = $this->lc->getOne(array('level' => $data['level']));
            if (!empty ($item))
                show_error('此等级已经存在!');
        }


        $rs = $this->lc->save($data, $lid ? $lid : null);
        if (!$rs)
            show_error('保存失败!');

        $this->load->helper('url');
        redirect(url('admin').'User_level/index');
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $lid = (int)$this->input->get_post('lid');

            if (!$lid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_user_level_config', 'lc');
            $item = $this->lc->getOne(array('lid' => $lid));

            if (empty ($item))
            {
                $response = error(200000);
                break;
            }


            $res = $this->lc->delete(array('lid' => $lid));
            if (!$res)
            {
                $response = error(70004);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}